Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • stata program - options fail when calling `0'

    Dear all,

    I am writing a stata program in which I would like to take as arguments a list of numerical values and additonally have optional options. I want to display all inputs (later I want to export them to pyhton as an array with the python modul).

    Once, I call the argument `0' which shows all inputs, the optional inputs are not known anymore.

    Code:
    program define test5
    version 17
    syntax anything [, Number(int 5)]  
    dis `0'  
    dis `number'
    end
    If I run the following code, there is no problem:

    Code:
    test5 1 1
    Output
    Code:
    11
    5
    Once I specify an option I get an error:

    Code:
    test5 1 1, number(8)
    Output:
    Code:
    11 unknown function number()
    Does anyone know what is happening here or where I might find some information on how to deal with this problem?

    Best,
    Jan



  • #2
    Code:
    dis "`0'"
    will do.

    Comment


    • #3
      Code:
      program define test5
      version 17
      syntax anything [, Number(int 5)]  
      dis `"`0'"'  
      dis `number'
      end

      Comment


      • #4
        display has two extreme roles, (1) to display with little or no interpretation and (2) to calculate and then display with maximum interpretation.

        At its simplest, the difference is shown by

        Code:
        . di "2 + 2"
        2 + 2
        
        . di 2 + 2
        4
        The quotation marks are fundamental, not ornamental or redundant.
        Last edited by Nick Cox; 16 Aug 2023, 13:45.

        Comment


        • #5
          Thank you all, that solved the problem!

          Just for completeness, once I included the quotation marks like all answers have suggested, the output becomes:

          Code:
          . program define test6
            1. version 17
            2. syntax anything [, Number(int 5)]
            3. dis "`0'"
            4. dis `number'
            5. end
          
          . 
          . test6 1 1, number(6)
          1 1, number(6)
          6

          Comment


          • #6
            Let me just suggest that you would be better off using compound double quotes, as I showed in #3. The use of regular double quotes will work, provided that the contents of `0' does not, itself, contain any quotation marks. But with -syntax anything-, you are by no means guaranteed that quotation marks will not appear. If you use the compound double quotes, the code will work as you intend even if `0' does contain some quotation marks of its own. If not, the code will fail. In general, you should prefer `" "' to " " in Stata, except in contexts where you are certain that no quotes will be embedded within.

            Comment

            Working...
            X